package com.bosicloud.cmb.ibp.aws.adapter.api;

import com.bosicloud.cmb.ibp.aws.adapter.commom.constant.UrlDefine;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Jack Peng / ziliang.peng@bosicloud.com
 */
@FeignClient(value = "AWSAdapterService", configuration = AWSS3Client.MultipartSupportConfig.class, url = "${ibp.service.aws-adapter-url}")
public interface AWSS3Client {


    @PostMapping(value = UrlDefine.S3_UPLOAD,
            produces = { MediaType.TEXT_PLAIN_VALUE },
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String upload(@RequestPart(value = "file") MultipartFile file);

    class MultipartSupportConfig {

        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public feign.codec.Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }

}
